<template>
  <div>
    <!--需要配置到这个页面才行-->
    微信企业登录中,请稍后........
  </div>
</template>
<script>
  import Vue from 'vue'
  import { ACCESS_TOKEN, USER_NAME,USER_INFO} from "@/store/mutation-types"
  import { getQueryString,welcome } from '@/utils/util'
  import { qywxLogin } from '@/api/login'
  import urlencode from "urlencode"
  export default {
    name: 'QywxLogin',
    created() {
  /*    console.log("-----------------------------")
      console.log("-----------------------------")

      let redirect_uri = urlencode('http://o2oa.gdztyy.com/third/qywx')
      let corp_id = "wwc57cdfb0d3909158"
      let agent_id = '1000003'
      console.log(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${corp_id}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_base&agentid=${agent_id}&state=STATE&connect_redirect=1#wechat_redirect`)
*/
      let code = getQueryString('code')
      if(!code){
        alert(code)
        return;
      }
      qywxLogin({
        'code':code
      }).then(response => {
        if(response.code =='200'){
          const result = response.result
          const userInfo = result.userInfo
          Vue.ls.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000)

          Vue.ls.set(USER_NAME, userInfo.username, 7 * 24 * 60 * 60 * 1000)
          Vue.ls.set(USER_INFO, userInfo, 7 * 24 * 60 * 60 * 1000)
          this.$store.commit('SET_TOKEN', result.token)
          this.$store.commit('SET_INFO', userInfo)
          this.$store.commit('SET_NAME', { username: userInfo.username,realname: userInfo.realname, welcome: welcome() })
          this.$store.commit('SET_AVATAR',  userInfo.avatar)
          if (response.success) {
            this.loginSuccess()
          } else {
            this.requestFailed(res)
          }
        }else{
          this.requestFailed(res)
        }
      }).catch(error => {
        this.requestFailed(res)
      })
    },
    methods:{
      loginSuccess () {


        // update-begin- author:sunjianlei --- date:20190812 --- for: 登录成功后不解除禁用按钮，防止多次点击
        // this.loginBtn = false
        // update-end- author:sunjianlei --- date:20190812 --- for: 登录成功后不解除禁用按钮，防止多次点击
        this.$router.push({ path: "/dashboard/analysis" }).catch(()=>{
          console.log('登录跳转首页出错,这个错误从哪里来的')
        })
        this.$notification.success({
          message: '欢迎',
          description: `${timeFix()}，欢迎回来`,
        });
      },
      requestFailed (err) {
      this.$notification[ 'error' ]({
        message: '登录失败',
        description: ((err.response || {}).data || {}).message || err.message || "请求出现错误，请稍后再试",
        duration: 4,
      });
      this.loginBtn = false;
    },
    }
  }
</script>

<style scoped>

</style>